var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: '',
    //显示数据
    patient: [],
    scrollHeight: 0,
    inputVal: '',
    flag: false,
    patient1: [{
      logoLink: '/images/defaultImage.png',
      patientName: '小红',
      visitNum: '4',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小明',
      visitNum: '3',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小白',
      visitNum: '5',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小刘',
      visitNum: '1',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小兰',
      visitNum: '21',
      patientTel: "18727508188"
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();

    this.getPatient();
    this.setData({
      username: getApp().globalData.username,
    })
    /* try {
      var ap = wx.getStorageSync('distinctPatient')
      this.setData({
        patient: ap
      })
    } catch(e) { } */

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPatient()
    this.enterPage(6);
    console.log(6)

    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('patients')
      this.setData({
        patient: ap
      })
    } catch(e) { }
  },

   /**
   * 进入医生端 patients页面，通知后端将进入此页面次数加一
   */
  enterPage: function (e) {
    var that = this
    wx.request({
      url: this.data.url + 'patientsettablebar',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        unitphone: getApp().globalData.username,
        pageNum: e
      },
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getPatient()
  },

  /**
   * 显示输入框
   */
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  
  /**
   * 隐藏输入框
   */
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      flag: false
    });
    this.getPatient()
  },

  /**
   * 清空输入框内容
   */
  clearInput: function () {
    this.setData({
      inputVal: "",
    });
  },
  
  /**
   * 记录输入
   */
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value,
      flag: true,
    });
    this.getPatient()
  },

  /**
   * 获取病人信息
   */
  getPatient: function () {
    var that = this
    var id = this.data.username
    var flag = this.data.flag
    var name = this.data.inputVal
    wx.request({
      url: this.data.url + 'getEachPatient',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        flag: flag,
        patientName: name,
        doctorID: id
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") {
          try {
            wx.setStorageSync("distinctPatient", res.data)
          } catch (e) { }
          that.setData({
            patient: res.data
          })
        }
        else if(res.data == "空") {
          that.setData({
            patient: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 跳转病人信息
   */
  clickPatient: function(e) {
    console.log(1)
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    console.log(that.data.patient[index])
    var queryBean = JSON.stringify(that.data.patient[index].patientTel)
    console.log(queryBean)
    wx.navigateTo({
      url: '../detailedPatient/detailedPatient?queryBean=' + queryBean,
    })
  },

  /**
   * 医生点击与患者交谈
   */
  chatWithPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var queryBean = JSON.stringify(this.data.patient[index].patientTel)
    var queryBean2 = JSON.stringify(this.data.patient[index].patientName)
    wx.navigateTo({
      url: '../onlineConsulting/onlineConsulting?otherTel=' + queryBean + '&&otherName=' + queryBean2,
    })
  }
})